#include "sys.h"
#include "log.h"
#include "dial.h"
#include "cJSON.h"
#include "traceroute_tactics.h"
#include "kafka.h"

static void _get_ip_latency_list_string(tr_result_t* result, char* iplist, char* latencylist)
{
    int i = 0;
    char ip_str[32] = { 0 }, latency_str[32] = { 0 };

    for (i = 0; i < result->count; i++) {
        sprintf(ip_str, "%s,", inet_ntoa(*((struct in_addr*)&result->ret[i].ip)));
        sprintf(latency_str, "%.2f,", result->ret[i].latency);
        strcat(iplist, ip_str);
        strcat(latencylist, latency_str);
    }

    // if()
    // i++;
    // sprintf(ip_str, "%s", inet_ntoa(*((struct in_addr*)&result->ret[i].ip)));
    // sprintf(latency_str, "%.2f", result->ret[i].latency);
    // strcat(iplist, ip_str);
    // strcat(latencylist, latency_str);
}

void* do_traceroute_tactics(void* arg)
{
    traceroute_tactics_t* traceroute = (traceroute_tactics_t*)arg;
    tracerouteTactics_t* ptr;
    struct tracerouteTactics_head* head = &traceroute->head;
    cJSON *result, *obj, *arr;

    dial_arg_t dial_arg = { 0 };
    dial_result_t* dial_result = NULL;
    tr_result_t* tr_result = NULL;
    char topic[32];
    char ip_list_str[512] = { 0 }, latency_list_str[512] = { 0 };
    char *post_data = NULL, reportTime[32], collectTime[32];

    if (traceroute->count == 0)
        return NULL;
    result = cJSON_CreateObject();
    cJSON_AddStringToObject(result, "dialTaskId", traceroute->taskId);
    cJSON_AddStringToObject(result, "version", traceroute->version);
    cJSON_AddStringToObject(result, "indexType", "traceroute");
    get_timestring(reportTime);
    cJSON_AddStringToObject(result, "reportTime", reportTime);
    get_timestring(collectTime);
    cJSON_AddStringToObject(result, "collectTime", collectTime);
    // pthread_mutex_lock(&traceroute->mutex);
    arr = cJSON_CreateArray();
    TAILQ_FOREACH(ptr, head, entry)
    {
        obj = cJSON_CreateObject();
        dial_arg.in.tr_arg.ip = wd_ipaddr(ptr->toAddress);
        dial_arg.in.tr_arg.port = port_random(0);
        dial_arg.in.tr_arg.max_hop = ptr->maxNum;
        dial_arg.in.tr_arg.timeout = ptr->timeout;
        dial_arg.in.tr_arg.blocksize = ptr->size;
        dial_arg.in.tr_arg.opt = strcmp(ptr->type, "udp") == 0 ? OPT_PRO_UDP : OPT_PRO_ICMP;
        dial_result = (dial_result_t*)dial_traceroute(&dial_arg);
        tr_result_print(&dial_result->out.tr_result);

        tr_result = &dial_result->out.tr_result;
        cJSON_AddStringToObject(obj, "toAddress", ptr->toAddress);

        _get_ip_latency_list_string(tr_result, ip_list_str, latency_list_str);
        cJSON_AddStringToObject(obj, "tracIP", ip_list_str);
        cJSON_AddStringToObject(obj, "traceTime", latency_list_str);

        strcpy(topic, ptr->kafkaTopic);
        cJSON_AddStringToObject(obj, "businessTag", ptr->businessTag);
        cJSON_AddItemToArray(arr, obj);
        free(dial_result);
    }
    cJSON_AddItemToObject(result, "tracerouteList", arr);
    post_data = cJSON_Print(result);
    log_debug("Traceroute post_data:%s", post_data);
    kafka_send(post_data, strlen(post_data), traceroute->kafka_url, topic);
    free(post_data);
    cJSON_Delete(result);
    // pthread_cond_signal(&traceroute->cond);
    // pthread_mutex_unlock(&traceroute->mutex);
    return NULL;
}